require(["config"], function() {
	require(["jquery", "template","swiper", "sider","header-top"], function($, template,Swiper) {
		function Index() {
			this.loadHotProducts();
			this.swip();
			this.headerHandler();
		}
		$.extend(Index.prototype, {
			// 加载渲染热销商品
			
			loadHotProducts() {
				$.getJSON("http://rap2api.taobao.org/app/mock/120150/api/hot", (data)=>{
					// console.log(data)
					// var htmlstring = template("模板id", 待渲染数据data);
					const html = template("prod-template", {products: data.src_body.list})
					// console.log(html);
					// console.log($("ul.grid_2"))
					$("ul.grid_2").html(html);
				});				
			},
			//轮播图
			swip(){
				var mySwiper1 = new Swiper('.swiper-container', {
					autoplay: true,//可选选项，自动滑动
					loop : true,//循环
					// 如果需要分页器
					pagination: {
						el: '.swiper-pagination',
						clickable :true, //点击分页器控制
					},
					//左右按钮
					navigation: {
						nextEl: '.swiper-button-next',
						prevEl: '.swiper-button-prev',
					},
				})
			},
			
			//添加头部交互
		   	headerHandler() {
				
				// 搜索提示
				$(".search :text").on("keyup", this.suggestHandler);
				// 点击提示：事件委派
				$(".suggest").on("click", "div", (event) => {
					$(".search :text").val($(event.target).text());
					// <==>
					// $(".search :text")[0].value = event.target.innerText;
					// 隐藏提示
					$(".suggest").hide(); // $(".suggest")[0].style.display = "none"
				})
			},
			// 搜索提示
			suggestHandler(event) {
				console.log(11);
				const
					word = $(event.target).val(), // 从文本框中获取输入值
					url = `https://suggest.taobao.com/sug?code=utf-8&q=${word}&callback=?`; // jsonp接口URL
				// jsonp跨域请求淘宝建议接口
				$.getJSON(url, (data) => {
					console.log(data)
					let html = "";
					data.result.forEach((curr) => {
						html += `<div>${curr[0]}</div>`;
					});
					$(".suggest").show().html(html);
				});
			}
			
		});
		new Index();
	});
});
